<?php

namespace App\Extensions;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Support\Str;

/**
 * Created by PhpStorm.
 * User: zhanghua
 * Date: 18-7-30
 * Time: 下午12:52
 */
class Md5eloquentProviders extends EloquentUserProvider
{

	/**
	 * Retrieve a user by the given credentials.
	 *
	 * @param  array  $credentials
	 * @return \Illuminate\Contracts\Auth\Authenticatable|null
	 */
	public function retrieveByCredentials(array $credentials)
	{
		if (empty($credentials) ||
			(count($credentials) === 1 &&
				array_key_exists('password', $credentials))) {
			return;
		}

		// First we will add each credential element to the query as a where clause.
		// Then we can execute the query and, if we found a user, return it in a
		// Eloquent User "model" that will be utilized by the Guard instances.
		$query = $this->createModel()->newQuery();

		foreach ($credentials as $key => $value) {
			if (! Str::contains($key, 'password')) {
				$query->where($key, $value);
			}
		}

		return $query->first();
	}

	/**
	 * Validate a user against the given credentials.
	 *
	 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
	 * @param  array  $credentials
	 * @return bool
	 */
	public function validateCredentials(UserContract $user, array $credentials)
	{

		$plain = $credentials['password'];
		$salt = $user->getSalt();
		return $user->getAuthPassword() === md5(md5($plain) . $salt);

		$plain = $credentials['password'];

		return $this->hasher->check($plain, $user->getAuthPassword());
	}

}